package cn.edu.nbpt.campus_api.controller.campus;

import cn.edu.nbpt.campus_api.model.Page;
import cn.edu.nbpt.campus_api.model.Result;
import cn.edu.nbpt.campus_api.model.campus.entity.Student;
import cn.edu.nbpt.campus_api.service.campus.StudentService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("campus/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @GetMapping("list")
    public Result<Page<Student>> selectStudentList(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) Date birthdayStartTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) Date birthdayEndTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) Date entryStartTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) Date entryEndTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) Date graduationStartTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @RequestParam(required = false) Date graduationEndTime,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ) {
        return Result.success(studentService.selectStudentList(
                name, username, gender,
                birthdayStartTime, birthdayEndTime,
                entryStartTime, entryEndTime,
                graduationStartTime, graduationEndTime,
                pageNum, pageSize
        ));
    }

    @GetMapping("{id}")
    public Result<Student> selectStudent(
            @PathVariable Integer id
    ) {
        return Result.success(studentService.selectStudent(id));
    }

    @PostMapping
    public Result<Integer> insertStudent(
            @RequestBody Student student
    ) {
        return Result.success(studentService.insertStudent(student));
    }

    @PutMapping
    public Result<Integer> updateStudent(
            @RequestBody Student student
    ) {
        return Result.success(studentService.updateStudent(student));
    }

    @DeleteMapping("{ids}")
    public Result<Integer> deleteStudent(
            @PathVariable List<Integer> ids
    ) {
        return Result.success(studentService.deleteStudent(ids));
    }
}
